package com.queclink.ycm.model;

import com.queclink.ycm.common.utils.DataUtil;
import io.netty.channel.ChannelHandlerContext;

/**
 * @author jim.li
 * @email jim.li@queclink.com
 * @date 2022-01-10 09:24:00
 */
public class ChannelSession {

    private ChannelHandlerContext ctx;
    private String uniqueId;
    private long time = System.currentTimeMillis();   //最新通信时间

    public ChannelHandlerContext getCtx() {
        return ctx;
    }

    public void setCtx(ChannelHandlerContext ctx) {
        this.ctx = ctx;
    }

    public String getUnqiuedId() {
        return uniqueId;
    }

    public void setUnqiuedId(String uniqueId) {
        this.uniqueId = uniqueId;
    }

    public long getTime() {
        return time;
    }

    public void setTime(long time) {
        this.time = time;
    }

    public void sendClientMsg(String message) {
        this.setTime(System.currentTimeMillis());
        System.out.println(DataUtil.getNowFormatDate() + " [发送数据]" + message);
        if (this.getCtx() != null && this.getCtx().channel().isWritable()) {
            this.getCtx().writeAndFlush(message.getBytes());
        } else {
            System.out.println("通道不存在或者已关闭");
        }
    }
}
